﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Configuration;

namespace PLC
{
    public class EnvDataManager: IEnvDataManager
    {
        //int experimentType;
        //int pumpType;

        PLCManager plc = null;//= PLCManager.GetInstance();
        ModBusRTUManager modBus = null;// = ModBusRTUManager.GetInstance();
        SerialPortManager serialPortManager = null;// = SerialPortManager.GetInstance();
        
         
        private void initManager()
        {
            //plc = PLCManager.GetInstance(PLCConfig.IP, PLCConfig.Rack, PLCConfig.Slot, PLCConfig.ConnectionType);
            modBus = ModBusRTUManager.GetInstance(ModBusConfig.PortName, ModBusConfig.BaudRate, ModBusConfig.Parity, ModBusConfig.DataBits, ModBusConfig.StopBits, ModBusConfig.ReadTimeOut);
            if (SerialPortConfig.HasSerialPortSection)
            {
                serialPortManager = SerialPortManager.GetInstance(SerialPortConfig.Port, SerialPortConfig.BaudRate, SerialPortConfig.Parity, SerialPortConfig.DataBits, SerialPortConfig.StopBits);
            }
        }


        PLCManager plc = PLCManager.GetInstance();
        ModBusRTUManager modBus = ModBusRTUManager.GetInstance();
        static EnvDataManager envDataManager=null;
        private EnvDataManager()
        {
            initManager();
        }
        public static EnvDataManager GetInstance()
        {
            if (envDataManager == null)
            {
                envDataManager = new EnvDataManager();

            }
            return envDataManager;
        }

        Boolean isConnected=false;

        public bool IsConnected { get => isConnected; }


        public void Connect()
        {
            if (plc != null)
            {
            if (!plc.IsConnected)
            {
                    int ret = plc.Connect();

                }
            }
            if(modBus!=null)
            {
            if (!modBus.IsOpen)
            {
                modBus.Open();
            }
            }
            if (serialPortManager != null)
            {
                if (serialPortManager.IsOpen)
                {
                    serialPortManager.Open();
                }
            }

            isConnected = true;
        }

        public void Close()
        {
            if(plc!=null)
            {
            if (plc.IsConnected)
            {
                plc.Close();
            }
            }
            if (modBus != null)
            {
            if (modBus.IsOpen)
            {
                modBus.Close();
            }
            }
            if (serialPortManager != null)
            {
                if (serialPortManager.IsOpen)
                {
                    serialPortManager.Close();
                }
            }
            isConnected = false;
        }

        private double getValue(ushort data1, ushort data2)
        {
            byte[] b = new byte[4];
            b[0] = (byte)(data1 >> 8);
            b[1] = (byte)data1;
            b[2] = (byte)(data2 >> 8);
            b[3] = (byte)data2;
            /// byte[] byteTemp = new byte[4] { b[0], b[1], b[2], b[3] };
            byte[] byteTemp = new byte[4] { b[3], b[2], b[1], b[0] };

            return (double)BitConverter.ToSingle(byteTemp, 0);
        }
        /// <summary>
        /// 读取数据，需要传入电流变比,后续电流变比也可读
        /// </summary>
        /// <param name="currentTransformationRatio">电流变比</param>
        /// <returns></returns>
        public EnvData ReadData(int dn,double transformationRatio)
        {
            double presureIn =  plc.readVData(100);
            double presureOut = plc.readVData(110);
            
            double flow = plc.readVData(dn); 
            //plc.readVData(experiment.FlowmeterAddress);流量计地址选择，这里选择也没有做
            double temperature = plc.readVData(180);
            double speed = plc.readVData(190);

            ushort[] meanData = modBus.ReadHoldingRegisters(1, 544, 20);

            double voltage = getValue(meanData[0], meanData[1]);
            double current = getValue(meanData[2], meanData[3]) * transformationRatio;
            double activePower = getValue(meanData[4], meanData[5]) * transformationRatio;
            //float wggl = getValue(meanData[6], meanData[7])*transformationRatio;//无功功率
            //float szgl = getValue(meanData[8], meanData[9])*transformationRatio;//视在功率
            double powerfactor = getValue(meanData[10], meanData[11]);   //功率因素

            #region 
            /*
            ushort[] meanData = modBus.ReadHoldingRegisters(1, 111, 72);

            double voltage = getValue(meanData[52], meanData[1]);
            double current = getValue(meanData[2], meanData[3]) * transformationRatio;
            double activePower = getValue(meanData[4], meanData[5]) * transformationRatio;
            //float wggl = getValue(meanData[6], meanData[7])*transformationRatio;//无功功率
            //float szgl = getValue(meanData[8], meanData[9])*transformationRatio;//视在功率
            double powerfactor = getValue(meanData[10], meanData[11]);   //功率因素
            */
            #endregion


            EnvData data = new EnvData(presureIn, presureOut, flow, temperature, speed, voltage, current, activePower, powerfactor);

           
            return data;
        }

        /// <summary>
        /// 开发环境不需要管，为了测试方便
        /// </summary>
        public void NextData_Test()
        {

        }

        /// <summary>
        /// 开发环境不需要管，为了测试方便
        /// </summary>
        public void SetIndexZero()
        {
        
        }

    
    }
}
